<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"  xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<meta charset="utf-8">
<head th:include="centre/manage/include :: header">
</head>

<link rel="stylesheet" type="text/css" href="/css/plugins/layui/layui.css"/>

<style>
input[type=email], input[type=url], input[type=search], input[type=tel],
	input[type=color], input[type=text], input[type=password], input[type=datetime],
	input[type=datetime-local], input[type=date], input[type=month], input[type=time],
	input[type=week], input[type=number], textarea {
	border-radius: 0 !important;
	color: #858585;
	background-color: #FFF;
	border: 1px solid #D5D5D5;
	padding: 5px 4px 6px;
	font-size: 14px;
	font-family: inherit;
	-webkit-box-shadow: none !important;
	box-shadow: none !important;
	-webkit-transition-duration: .1s;
	transition-duration: .1s;
}

.table.table-bordered tr th, .table.table-bordered tr td {
	overflow: hidden;
	text-overflow: ellipsis;
}
select {
    padding: 3px 4px;
    height: 30px;
}
</style>
<body class="gray-bg">
	<div class="wrapper wrapper-content">
		<div class="row">
			<div class="col-sm-12">
			<div class="panel panel-info">
				<div class="panel-heading">商品销量统计列表</div>
			    <div class="panel-body">
			        <div class="form-horizontal">
		             <div class="form-group" style="padding-left:60px;">
		              <div class="col-xs-12 col-sm-2 hidden-xs btn-sespan" >
						<div class="input-group">
							<span class="input-group-addon" style="height:30px;">
								付款时间
							</span>
							<input type="text" name="payStartTime" id="payStartTime" 
								value="" placeholder="开始时间" 
								readonly class="layui-input form-control"
								style="height:30px;"/>
							<span>
								<i class="fa fa-exchange"></i>
							</span>
							<input type="text"  name="payEndTime" id="payEndTime" 
								value="" placeholder="结束时间" 
								readonly class="layui-input form-control"
								style="height:30px;"/>
	 					
						</div>
					 </div>
					 <div class="col-xs-12 col-sm-2 hidden-xs btn-sespan" >
	                   <select class="form-control tpl-category-parent" id="pcate"
							name="pcate" onchange="renderCategoryThird(this)">
							<option value="">顶级分类</option>
							<option th:each="item: ${category}" th:text="${item.name}"
								th:value="${item.id}"></option>
								
						</select>
						<select class="form-control tpl-category-child" id="ccate"
							name="ccate">
							<option value="">下级分类</option>
						</select>
		             </div>
		             <div class="col-xs-12 col-sm-2 hidden-xs btn-sespan" data-toggle="distpicker" id="dist_target">
	                    <select class="form-control tpl-category-parent" id="province" name="province" data-province="----选择省----">
						</select>
						<select class="form-control tpl-category-child"  id="city" name="city" data-city="----选择市----">
						</select>
		             </div>
					 <div class="col-xs-12 col-sm-1 hidden-xs btn-sespan" >
	                   <div class="input-group">
	                   		<select class="form-control" id="payState"
								name="payState">
								<option value="2">支付完成</option>
								<option value="0">未支付</option>
								<option value="1">支付中</option>
								<option value="3">支付失败</option>
								<option value="4">代付申请</option>
								<option value="5">待付中</option>
								<option value="6">代付成功</option>
								<option value="7">代付失败</option>
								<option value="8">退款申请</option>
								<option value="9">退款中</option>
								<option value="10">退款成功</option>
								<option value="11">退款失败</option>
							</select>
						</div>
		             </div>
		              <div class="col-xs-12 col-sm-1 hidden-xs btn-sespan" >
	                   <div class="input-group">
	                   		<select class="form-control" id="status" name="status">
								<option value="">-订单状态-</option>
								<option value="1">待发货</option>
								<option value="2">待收货</option>
								<option value="3">已收货</option>
								<option value="4">待退款</option>
								<option value="5">售后中</option>
								<option value="6">已完成</option>
								<option value="-1">已取消</option>
								<option value="0">未支付</option>
							</select>
						</div>
		             </div>
		             
		             <div class="col-xs-12 col-sm-2 hidden-xs btn-sespan">
							<select data-placeholder="商品区分" class="form-control chosen-select" name="baseId"
							 id="baseId" tabindex="2" >
							 <option value="" selected="selected">-- 请选择 --</option>
								<option  value="0">平台商品</option>
								<option value="1">商户商品</option>
							</select>
						</div>
		             
		                <div class="col-xs-12 col-sm-2 hidden-xs btn-sespan">
							<select data-placeholder="快递直发商品" class="form-control chosen-select" name="isKuaidi"
							 id="isKuaidi" tabindex="2" >
							 <option value="" selected="selected">-- 请选择快递直发 --</option>
								<option  value="0">普通商品</option>
								<option value="1">快递直发商品</option>
							</select>
						</div>
		             
		             
		             <div class="col-xs-12 col-sm-2 hidden-xs btn-sespan" >
	                   <select class="form-control tpl-category-parent"  id="supplyName">
							<option value="0">按商品名称</option>
							<option value="1">按供应商</option>
						</select>
						<input name="title" id="title" type="text" class="form-control"
								value="" placeholder="输入搜索内容">
		             </div>
		             <input type="hidden" id="flag" th:value="${flag}">
		             <input type="hidden" id="centreId" name="centreId" th:value="${centreId}" th:if="${flag} == '1'">
					 <div class="col-xs-12 col-sm-2 hidden-xs btn-sespan" th:if="${flag} == '0'">
						<select data-placeholder="商户" class="form-control chosen-select" 
						   id="centreId" name="centreId" tabindex="2" onchange="renderCategoryThird2(this)">
							<option value="">选择商户</option>
							<option th:each="item: ${centreList}" th:text="${item.name}" th:value="${item.centreId}"></option>
						</select>
					 </div>
					 
					  <div class="col-xs-12 col-sm-2 hidden-xs btn-sespan" >
							<select  id="liansuoid"
								class="form-control chosen-select" tabindex="2">
								<option value="" selected="selected">选择门店</option>
								<option th:each="item: ${teams}" th:text="${item.tXiaoqu+ +item.id}"
                                        th:value="${item.id}"></option>
							</select>
						</div>
						
							<div class="col-xs-12 col-sm-2 hidden-xs btn-sespan">
							<select id="isSource"
								class="form-control chosen-select" tabindex="2">
								<option value >选择订单商品来源</option>
								  <option value="0" >非门店</option>
								  <option value="1" >门店</option>
							</select>
						</div>
					 
		            </div>
		            <!-- <div class="form-group">
		                <label class="col-xs-12 col-sm-2 col-md-2 col-lg-1 control-label">排序方式</label>
		                <div class="col-sm-8 col-lg-9 col-xs-12">
		                    <label class='radio-inline'>
		                        <input type='radio' name='orderby' value='0' /> 按销售额
		                    </label>
		                    
		                    <label class='radio-inline'>
		                        <input type='radio' name='orderby' value='1'  checked/> 按销售量
		                    </label>
		                    <label class='radio-inline'>
		                        <input type='radio' name='orderby' value='2'  /> 按付款时间
		                    </label>
		                </div>
		            </div> -->
		            
		            <div class="form-group">
		                <label class="col-xs-12 col-sm-2 col-md-2 col-lg-1 control-label"></label>
		                <div class="col-sm-8 col-lg-9 col-xs-12">
		                    <button class="btn btn-default" onclick="reLoad()">
		                    	<i class="fa fa-search"></i> 搜索
		                    </button>
		                    <button type="button" class="btn btn-default" 
		                    	style='margin-left:10px;' onclick="exportProductSaleList()">
		                    	<span class="fa fa-download"></span>
								导出销售统计表
							</button>
		                </div>
		            </div>
			        </div>
			    </div>
			</div>
			
			<div class="panel panel-default">
    			<div class="panel-body" style="padding: 0;">
    				<table id="productSaleListTable" data-mobile-responsive="true">
					</table>
					<table>
						<tr>
							<td height="50" colspan="7" align="right">当前页合计:  </td>
							<td align="center">
								<label class="btn btn-minier btn-info" id="total"> 0</label>
							</td>
							<td height="50" colspan="2" align="center"><label class="btn btn-minier btn-success" id="amount">0</label></td>
							<td height="50" colspan="2" align="center"><label class="btn btn-minier btn-success" id="costprice">0</label></td>
							<td height="50" colspan="2" align="center"><label class="btn btn-minier btn-success" id="commision">0</label></td>
							<td height="50" colspan="2" align="center"><label class="btn btn-minier btn-success" id="poundage">0</label></td>
							<td height="50" colspan="2" align="center"><label class="btn btn-minier btn-success" id="profitRate">0</label></td>
							<td height="50" colspan="2" align="center"><label class="btn btn-minier btn-success" id="profit">0</label></td>
							<td height="50" colspan="2" align="center"><label class="btn btn-minier btn-success" id="discountprice">0</label></td>
							<td height="50" colspan="2" align="center"><label class="btn btn-minier btn-success" id="deductprice">0</label></td>
							<td height="50" colspan="2" align="center"><label class="btn btn-minier btn-success" id="deductcredit2">0</label></td>
							<td height="50" colspan="2" align="center" th:if="${flag} == '0'"><label class="btn btn-minier btn-success" id="baseProfit">0</label></td>
						</tr>
						<tr>
							<td height="50" colspan="7" align="right">总合计:  </td>
							<td align="center">
								<label class="btn btn-minier btn-info" id="totalNum"> 0</label>
							</td>
							<td height="50" colspan="2" align="center"><label class="btn btn-minier btn-success" id="totalAmount"> 0 </label></td>
							<td height="50" colspan="2" align="center"><label class="btn btn-minier btn-success" id="totalcostprice"> 0 </label></td>
							<td height="50" colspan="2" align="center"><label class="btn btn-minier btn-success" id="totalcommision"> 0 </label></td>
							<td height="50" colspan="2" align="center"><label class="btn btn-minier btn-success" id="totalpoundage"> 0 </label></td>
							<td height="50" colspan="2" align="center"><label class="btn btn-minier btn-success" id="totalProfitRate"> 0 </label></td>
							<td height="50" colspan="2" align="center"><label class="btn btn-minier btn-success" id="totalprofit"> 0 </label></td>
							<td height="50" colspan="2" align="center"><label class="btn btn-minier btn-success" id="sumDiscountprice"> 0 </label></td>
							<td height="50" colspan="2" align="center"><label class="btn btn-minier btn-success" id="sumDeductprice"> 0 </label></td>
							<td height="50" colspan="2" align="center"><label class="btn btn-minier btn-success" id="sumDeductcredit2"> 0 </label></td>
							<td height="50" colspan="2" align="center" th:if="${flag} == '0'"><label class="btn btn-minier btn-success" id="sumbasecostprice"> 0 </label></td>
						</tr>
					</table>
    			</div>
			</div>
		</div>
			
		</div>
		</div>
	<div th:include="centre/manage/include::footer"></div>
	<script type="text/javascript" src="/public/ace/js/ace-extra.js"></script>
	<script type="text/javascript" src="/js/plugins/layui/layui.js"></script>
	<script type="text/javascript">
		function closeDow() {
			var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
			parent.layer.close(index);
		}
		
		layui.use('laydate', function(){
			  var laydate = layui.laydate;
			  laydate.render({
			  	elem: '#payStartTime' , type: 'datetime'
			  });
			  
			  laydate.render({
			  	elem: '#payEndTime' , type: 'datetime'
			  });
		})

		var prefix = "/report/manage";
		var flag;
		$(document).ready(function () {
			$("#province").val("");
			$("#city").val("");

            var date = new Date();
            var year = date.getFullYear();
            var mon=date.getMonth()+1 < 10 ? "0"+ (date.getMonth()+1+""):date.getMonth()+1;
            var da=date.getDate() < 10 ? "0"+ date.getDate():date.getDate();
            date.setDate(date.getDate()-1);
            var endyear = date.getFullYear();
            var endmon=date.getMonth()+1 < 10 ? "0"+ (date.getMonth()+1+""):date.getMonth()+1;
            var endda=date.getDate() < 10 ? "0"+ date.getDate():date.getDate();
            $("#payEndTime").val(year+"-"+mon+"-"+da+" 20:05:00");
            $("#payStartTime").val(endyear+"-"+endmon+"-"+endda+" 20:30:00");
            flag = $("#flag").val();
		    load();
		});
		function load() {
			$('#productSaleListTable').bootstrapTable({
				method : 'get', // 服务器数据的请求方式 get or post
				url : prefix + "/productSaleList", // 服务器数据的加载地址
				showRefresh : true,
				// showToggle : true,
				iconSize : 'outline',
				// toolbar: '#exampleToolbar',
				striped : true, // 设置为true会有隔行变色效果
				dataType : "json", // 服务器返回的数据类型
				pagination : true, // 设置为true会在底部显示分页条
				pageList : [ 10, 20, 50 ],
				// queryParamsType : "limit",
				// //设置为limit则会发送符合RESTFull格式的参数
				singleSelect : false, // 设置为true将禁止多选
				// contentType : "application/x-www-form-urlencoded",
				// //发送到服务器的数据编码类型
				pageSize : 10, // 如果设置了分页，每页数据条数
				pageNumber : 1, // 如果设置了分布，首页页码
				// search : true, // 是否显示搜索框
				showColumns : true, // 是否显示内容下拉框（选择显示的列）
				sidePagination : "server", // 设置在哪里进行分页，可选值为"client" 或者
				// "server"
				queryParams : function(params) {
					if($.trim($("#supplyName").val())==0){
                        var title = $.trim($("#title").val());
                    }else{
                        var supplyName = $.trim($("#title").val());
                    }
					return {
						limit : params.limit,
						offset : params.offset ,
						payStartTime: $.trim($("#payStartTime").val()),
						payEndTime: $.trim($("#payEndTime").val()),
						pcate: $.trim($("#pcate").val()),
						ccate: $.trim($("#ccate").val()),
						province: $.trim($("#province").val()),
						city: $.trim($("#city").val()),
						payState:$.trim($("#payState").val()),
						title:title,
                        supplyName:supplyName,
                        centreId:$.trim($("#centreId").val()),
					    status:$.trim($("#status").val()),
					    baseId:$.trim($("#baseId").val()),
					    isKuaidi:$.trim($("#isKuaidi").val()),
					    liansuoid:$.trim($("#liansuoid").val()),
                        isSource:$.trim($("#isSource").val())
					};
				},
				columns: [
                {
                    title: '商品名称',
                    field: 'productName',
                    width: 200
                },
                {
                    title: '一级分类',
                    field: 'pcateName'
                },{
                    title: '二级分类',
                    field: 'ccateName'
                },{
                    title: '供应商',
                    field: 'supplyName'
                },{
                    title: '买手',
                    field: 'buyrealnm'
                },{
                    title: '站点',
                    field: 'name'
                },{
                    title: '商品型号',
                    field: 'specTitle'
                },{
                    title: '商品Id',
                    field: 'productId'
                },{
                    title: '规格名称',
                    field: 'optionname'
                },{
                    title: '销售价',
                    field: 'marketprice'
                },{
                    title: '门店佣金',
                    field: 'headcommission'
                },{
                    title: '推荐佣金',
                    field: 'superiorcommission'
                },{
                    title: '成本价',
                    field: 'costprice'
                },{
                    title: '平台成本价',
                    field: 'basecostprice',
                    formatter: function (value, row, index) {
                        if (flag == 1) {
                        	return "你无权限查看"
                        }else if(flag == 0){
                        	 return value
                        }
                    }
                },{
                    title: '平台毛利',
                    field: 'profit',
                    formatter: function (value, row, index) {
                        if (flag == 1) {
                        	 return "你无权限查看"
                        }else if(flag == 0){
                        	return value
                        }
                    }
                },{
                    title: '上架时间',
                    field: 'upStartTime'
                },{
                    title: '销售数量',
                    field: 'sumTotal'
                },{
                    title: '销售金额',
                    field: 'sumPrice'
                },{
                    title: '销售毛利',
                    field: 'sumprofit',
                    formatter: function (value, row, index) {
                        if (flag == 1) {
                        	 return "你无权限查看"
                        }else if(flag == 0){
                        	return value
                        }
                    }
                },{
                    title: '商品区分',
                    field: 'baseproductid',
                    formatter: function (value, row, index) {
                    	if(row.isKuaidi ==1 ){
                    		return '<label class="label label-default">快递直发</label>'
                    	}else{
	                        if (value !="") {
	                            return '<label class="label label-default">平台商品</label>'
	                        }else if (value == 0) {
	                            return '<label class="label label-danger">商户商品</label>'
	                        }
                    	} 
                    }
                }],
                onPostBody: function(){
				    countNum();
				}
			});

        }
		function reLoad() {
			$('#productSaleListTable').bootstrapTable('refresh');
		}

		function countNum() {
            var limit=$('#productSaleListTable').bootstrapTable('getOptions').pageSize;
            var pageNum=$('#productSaleListTable').bootstrapTable('getOptions').pageNumber;
            var offset=limit*(pageNum-1);
			var payStartTime=$.trim($("#payStartTime").val());
			var payEndTime=$.trim($("#payEndTime").val());
			var pcate=$.trim($("#pcate").val());
			var ccate=$.trim($("#ccate").val());
			var province=$.trim($("#province").val());
			var city=$.trim($("#city").val());
			var payState=$.trim($("#payState").val());
			var title=$.trim($("#title").val());
			var centreId=$.trim($("#centreId").val());
			var status=$.trim($("#status").val());
			var  baseId = $.trim($("#baseId").val());
			var isKuaidi = $.trim($("#isKuaidi").val());
			var liansuoid = $.trim($("#liansuoid").val());
            var isSource = $.trim($("#isSource").val());
		    var title='';
            var supplyName='';
            if($.trim($("#supplyName").val())==0){
                title = $.trim($("#title").val());
            }else{
                supplyName = $.trim($("#title").val());
            }
			if(limit&&pageNum){
                $.ajax({
                    url: prefix+"/countProductSale",
                    data:{"limit":limit,"offset":offset,"payStartTime":payStartTime,"payEndTime":payEndTime,
                        "pcate":pcate,"ccate":ccate,"province":province,"city":city,"payState":payState,"title":title,"centreId":centreId,
                        "status":status,"baseId":baseId,"supplyName":supplyName,"isKuaidi":isKuaidi,
                        "liansuoid":liansuoid,"isSource":isSource},
                    dataType: "json",
                    success: function(data){
                        if(data){
                            $("#total").html(data.orderNum+" 单");
                            $("#amount").html(data.amount+" 元");
                            $("#totalNum").html(data.totalOrderNum+" 单");
                            $("#costprice").html("站点成本"+data.costprice+"元");
                            $("#commision").html("门店佣金"+data.commision+"元");
                            $("#totalcostprice").html("站点总成本"+data.totalcostprice+"元");
                            $("#totalcommision").html("门店总佣金"+data.totalcommision+" 元");
                            $("#totalpoundage").html("总手续费"+data.totalpoundage+" 元");
                            $("#poundage").html("手续费"+data.poundage+" 元");
                            $("#profit").html("站点利润"+data.profit+" 元");
                            $("#totalprofit").html("站点总利润"+data.totalprofit+" 元");
                            $("#totalProfitRate").html("商户商品平台提点总金额"+data.totalProfitRate+" 元");
                            $("#profitRate").html("商户商品平台提点金额"+data.profitRate+" 元");
                            $("#baseProfit").html("当前页平台毛利"+data.baseProfit+" 元");
                            $("#sumbasecostprice").html("总平台毛利"+data.sumbasecostprice+" 元");
                            $("#discountprice").html("优惠卷金额"+data.discountprice+" 元");
                            $("#deductprice").html("积分金额"+data.deductprice+" 元");
                            $("#deductcredit2").html("余额支付"+data.deductcredit2+" 元");
                            $("#sumDiscountprice").html("总优惠卷金额"+data.sumDiscountprice+" 元");
                            $("#sumDeductprice").html("总积分金额"+data.sumDeductprice+" 元");
                            $("#sumDeductcredit2").html("总余额支付"+data.sumDeductcredit2+" 元");
                            if(data.totalAmount){
                                $("#totalAmount").html(data.totalAmount+" 元");
							}else{
                                $("#totalAmount").html("0 元");
							}

                        }
                    }
                })
			}
        }
		
		function renderCategoryThird(obj) {
			var index = obj.options[obj.selectedIndex].value;
			var html = "<option>请选择下级分类</option>";
			if (index == 0) {
				$("#ccate").html(html);
			}else {
				$.ajax({
					url: "/category/manage/getChildByPar",
					data:{"id":index},
					dataType: "json",
					success: function(data){
						for(var i=0; i< data.length; i++){
							html += '<option value="'+data[i].id+'">'+data[i].name+'</option>';
						}
						$("#ccate").html(html);
					}
				})
			}
		}
		
		 function renderCategoryThird2(obj) {
				var index = obj.options[obj.selectedIndex].value;
				var html = "<option value="">请选择门店</option>";
				if (index == 0) {
					$("#liansuoid").html(html);
				}else {
					$.ajax({
						url: "/commanderList/manage/listCommanders",
						data:{"centreId":index},
						dataType: "json",
						success: function(data){
							for(var i=0; i< data.length; i++){
								html += '<option value="'+data[i].id+'">'+data[i].tXiaoqu+ data[i].id+'</option>';
							}
							$("#liansuoid").html(html);
						}
					})
				}
			}
		
		function exportProductSaleList() {
		    var title='';
            var supplyName='';
			var payStartTime = $.trim($("#payStartTime").val());
			var payEndTime = $.trim($("#payEndTime").val());
			var province = $.trim($("#province").val());
			var city = $.trim($("#city").val());
			var payState = $.trim($("#payState").val());
			var centreId = $.trim($("#centreId").val());
			var pcate = $.trim($("#pcate").val());
			var ccate = $.trim($("#ccate").val());
			var  baseId = $.trim($("#baseId").val());
			var isKuaidi = $.trim($("#isKuaidi").val());
			var liansuoid = $.trim($("#liansuoid").val());
            var isSource = $.trim($("#isSource").val());
			 if($.trim($("#supplyName").val())==0){
	                title = $.trim($("#title").val());
	            }else{
	                supplyName = $.trim($("#title").val());
	            }
		    window.location.href = prefix + "/exportProductSaleList?payStartTime=" + payStartTime + 
		    		"&payEndTime=" + payEndTime +"&province=" + province +"&city=" + city +
		    		"&payState=" + payState +"&pcate=" + pcate +"&centreId=" + centreId + 
		    		"&title=" + title + "&ccate=" + ccate+"&baseId="+baseId+"&supplyName="+supplyName+
		    		"&isKuaidi="+isKuaidi+"&liansuoid="+liansuoid+"&isSource="+isSource;
		}
	</script>
	
</body>
</html>
